/**
 * @file:   main_test.c
 * @note:   2013-2023,   < git.sochina.net/think3r >
 * @brief:  EDID函数测试文件
 * @author: think3r
 * @date    2018/1/6
 * @note:
 * @note \n History:
   1.日    期: 2018/1/6
     作    者: think3r
     修改历史: 创建文件
 */

/*----------------------------------------------*/
/*                 包含头文件                   */
/*----------------------------------------------*/
#include "stdio.h"
#include <stdlib.h>  
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>

#include "../inc/common.h"

#include "../inc/edid.h"
#include "../inc/edid_file.h"
#include "../inc/build_time.h"

/*----------------------------------------------*/
/*                 宏类型定义                   */
/*----------------------------------------------*/

#define __DEBUG__  

#ifdef __DEBUG__  
    #define DEBUG(format,...) printf("*["__FILE__ "  %s L:%04d]>   "format"",__FUNCTION__, __LINE__, ##__VA_ARGS__)  
#else  
    #define DEBUG(format,...)  
#endif


/*----------------------------------------------*/
/*                结构体定义                    */
/*----------------------------------------------*/

/*----------------------------------------------*/
/*                 函数声明                     */
/*----------------------------------------------*/

/*----------------------------------------------*/
/*                 全局变量                     */
/*----------------------------------------------*/

/*----------------------------------------------*/
/*                 函数定义                     */
/*----------------------------------------------*/

void printf_edid_data(void)
{
    UINT i =0;
    
    printf("\n********************** EDID DATA *************************\n");
    for( i = 0; i < sizeof(EDID_DATA); i++ )
    {          
        if((i>0 && i%10==0) || (i == sizeof(EDID_DATA)))
        {
            printf("\n");
        }
        
         printf("0x%02x  ", EDID_DATA[i]);
    }    
    printf("\n\n");
    
    
    fflush(stdout);
}



int main(void)
{
    UINT8 *buf = NULL;  
	UINT fileSize = 0, war_number = 8;  
	char function_C = 0;
	char srcfile[300]={0};  
	ERR err = ok;
	
    mk_Build_Date(&build_time);

    while (1)
	{
        fflush(stdin);        
        fflush(stdout);
        
	    printf("\n*************************************************\n"
	            "please input which function you want to switch:\n"
               "1. [read]    edid_data  frome  [bin]  file;\n"
               "2. [read]    edid_data  frome  [H]    file;\n"
               "3. [output]  edid_data  to     [bin]  file;\n"
               "4. [output]  edid_data  to     [H]    file;\n"
               "q  to quit/exit \n"
               "*************************************************\n");

//        while (isspace(function_C) || function_C == '\r')
//        {
////            function_C = getchar();            
//            scanf("%c",&function_C);            
//            DEBUG("\tfunction_C:[%c]\n", function_C);
//        }
//        
        
        fflush(stdout);
        fflush(stdin);

inputIsSpace:        
        function_C = getchar();

        fflush(stdin);

        if('q' == function_C )
        {
            break;
        }
        else if (isspace(function_C))
        {
            goto inputIsSpace;
        }
        
        DEBUG("\tfunction_C:[%c] %d\n", function_C, function_C);
        
        printf("please input file path\n");          
        fflush(stdout);
        
        scanf("%s",srcfile);
        fflush(stdin);
        
        if('1' == function_C || '2' == function_C)
        {
            err = GetFileSize(srcfile, &fileSize);
            DEBUG("file:[%s]  fileSize:[%d]  err:[0x%x]\n", srcfile, fileSize, err);
        }
        
        if('1' == function_C)
        {
            err = read_bin(srcfile, EDID_DATA, EDID_DATA_LEN);                
            DEBUG("\t readEDIDFrom_H_file  err:{0x%x}\n", err);                
            printf_edid_data();
        }
        else if( '2' == function_C)
        {
            err = readEDIDFrom_H_file(srcfile, EDID_DATA, fileSize, EDID_DATA_LEN);
            DEBUG("\t readEDIDFrom_H_file  err:{0x%x}\n", err);                
            printf_edid_data();
        }
        else if( '3' == function_C)
        {
            err = writeEDIDdata2_bin_Files(srcfile, EDID_DATA, EDID_DATA_LEN);
            DEBUG("\t writeEDIDdata2_bin_Files  err:{0x%x}\n", err);  
        }
        else if( '4' == function_C)
        {
            printf("please input war nomber\n");  
            scanf("%d",&war_number);            
            fflush(stdin);
            
            err = writeEDIDdata2_H_file(srcfile, EDID_DATA, sizeof(EDID_DATA), war_number, (BOOL)1);
            DEBUG("\t writeEDIDdata2_H_file  err:{0x%x}\n", err);  
        }       
                
	}

    printf("This is a character control test!\n" );
    sleep(3);
    Cprintf_red("This is a test [%s   L:%d]\n", __FUNCTION__, __LINE__);    
    
#if 0    
    printf("[%2u]" CLEAR "CLEAR\n" NONE, __LINE__);

    printf("[%2u]" BLACK "BLACK " L_BLACK "L_BLACK\n" NONE, __LINE__);
    printf("[%2u]" RED "RED " L_RED "L_RED\n" NONE, __LINE__);
    printf("[%2u]" GREEN "GREEN " L_GREEN "L_GREEN\n" NONE, __LINE__);
    printf("[%2u]" BROWN "BROWN " YELLOW "YELLOW\n" NONE, __LINE__);
    printf("[%2u]" BLUE "BLUE " L_BLUE "L_BLUE\n" NONE, __LINE__);
    printf("[%2u]" PURPLE "PURPLE " L_PURPLE "L_PURPLE\n" NONE, __LINE__);
    printf("[%2u]" CYAN "CYAN " L_CYAN "L_CYAN\n" NONE, __LINE__);
    printf("[%2u]" GRAY "GRAY " WHITE "WHITE\n" NONE, __LINE__);

    printf("[%2u]\e[1;31;40m Red \e[0m\n",  __LINE__);

    printf("[%2u]" BOLD "BOLD\n" NONE, __LINE__);
    printf("[%2u]" UNDERLINE "UNDERLINE\n" NONE, __LINE__);
    printf("[%2u]" BLINK "BLINK\n" NONE, __LINE__);
    printf("[%2u]" REVERSE "REVERSE\n" NONE, __LINE__);
    printf("[%2u]" HIDE "HIDE\n" NONE, __LINE__);

    printf("Cursor test begins!\n" );
    printf("=======!\n" );
    sleep(10);
    printf("[%2u]" "\e[2ACursor up 2 lines\n" NONE, __LINE__);
    sleep(10);
    printf("[%2u]" "\e[2BCursor down 2 lines\n" NONE, __LINE__);
    sleep(5);
    printf("[%2u]" "\e[?25lCursor hide\n" NONE, __LINE__);
    sleep(5);
    printf("[%2u]" "\e[?25hCursor display\n" NONE, __LINE__);
    sleep(5);

    printf("Test ends!\n" );
    sleep(3);
    printf("[%2u]" "\e[2ACursor up 2 lines\n" NONE, __LINE__);
    sleep(5);
    printf("[%2u]" "\e[KClear from cursor downward\n" NONE, __LINE__);
#endif

	return err;
	
}
